home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / os2 / gnuwget.zip / wget-1.4.3 / src / url.c < prev    next >
C/C++ Source or Header  |  1997-02-09  |  36KB  |  1,448 lines

  1. /* URL handling.
  2.    Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
  3.    
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. #  include <config.h>
  21. #endif /* HAVE_CONFIG_H */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #ifdef HAVE_STRING_H
  26. #  include <string.h>
  27. #else
  28. #  include <strings.h>
  29. #endif
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #ifdef HAVE_UNISTD_H
  34. #  include <unistd.h>
  35. #endif
  36. #include <errno.h>
  37. #include <assert.h>
  38.  
  39. #include "wget.h"
  40. #include "options.h"
  41. #include "utils.h"
  42. #include "url.h"
  43. #include "host.h"
  44. #include "ftp.h"
  45. #include "mtch.h"
  46. #include "html.h"
  47.  
  48. extern struct options opt;
  49. extern int errno;
  50.  
  51. /* NULL-terminated list of strings to be recognized as prototypes (URL
  52.    schemes). Note that recognized doesn't mean supported -- only HTTP
  53.    and FTP are supported for now.
  54.  
  55.    However, a string that does not match anything in the list will be
  56.    considered a relative URL.  Thus it's important that this list has
  57.    anything anyone could think of being legal.
  58.    
  59.    There are wild things here. :-) Take a look at
  60.    <URL:http://www.w3.org/pub/WWW/Addressing/schemes.html> to see more
  61.    fun.  */
  62. char *protostrings[] = {
  63.    "cid:",
  64.    "clsid:",
  65.    "file:",
  66.    "finger:",
  67.    "ftp:",
  68.    "gopher:",
  69.    "hdl:",
  70.    "http:",
  71.    "ilu:",
  72.    "ior:",
  73.    "irc:",
  74.    "java:",
  75.    "javascript:",
  76.    "lifn:",
  77.    "mailto:",
  78.    "mid:",
  79.    "news:",
  80.    "nntp:",
  81.    "path:",
  82.    "prospero:",
  83.    "rlogin:",
  84.    "service:",
  85.    "shttp:",
  86.    "snews:",
  87.    "stanf:",
  88.    "telnet:",
  89.    "tn3270:",
  90.    "wais:",
  91.    "whois++:",
  92.    NULL
  93. };
  94.  
  95. /* Similar to former, but for supported protocols: */
  96. proto_t sup_protos[] = {
  97.    { "http://", URLHTTP, DEFAULT_HTTP_PORT },
  98.    { "ftp://", URLFTP, DEFAULT_FTP_PORT },
  99.    /*{ "file://", URLFILE, DEFAULT_FTP_PORT },*/
  100.    { NULL, FTPOK, 0 }
  101. };
  102.  
  103. /* Returns the number of characters to be skipped if the first thing
  104.    in a URL is URL: (which is 0 or 4+). The optional spaces after URL:
  105.    are also skipped. */
  106. int
  107. skip_url(const char *url)
  108. {
  109.    int i;
  110.    
  111.    if (toupper(url[0]) == 'U'
  112.        && toupper(url[1]) == 'R'
  113.        && toupper(url[2]) == 'L'
  114.        && url[3] == ':')
  115.    {
  116.       /* Skip blanks. */
  117.       for (i = 4; url[i] && isspace(url[i]); i++);
  118.       return i;
  119.    }
  120.    else
  121.       return 0;
  122. }
  123.  
  124. /* Returns 1 if the string contains unsafe characters, 0 otherwise. */
  125. int
  126. contains_unsafe(const char *s)
  127. {
  128.    for (; *s; s++)
  129.       if (strchr(URL_UNSAFE, *s))
  130.      return 1;
  131.    return 0;
  132. }
  133.  
  134. /* Decodes the forms %xy in a URL to the character the hexadecimal
  135.    code of which is xy. xy are hexadecimal digits from
  136.    [0123456789ABCDEF] (case-insensitive). If x or y are not hex-digits
  137.    or '%' is near '\0', the whole sequence is inserted literally. */
  138. void
  139. decode_string(char *s)
  140. {
  141.    char *p = s;
  142.  
  143.    for (; *s; s++, p++)
  144.    {
  145.       if (*s != '%')
  146.      *p = *s;
  147.       else
  148.       {
  149.      /* Do nothing if at the end of the string. Or if the chars
  150.         are not hex-digits. */
  151.      if (!*(s + 1) || !*(s + 2)
  152.          || !(isxdigit(*(s + 1)) && isxdigit(*(s + 2))))
  153.      {
  154.         *p = *s;
  155.         continue;
  156.      }
  157.      *p = (ASC2HEXD(*(s + 1)) << 4) + ASC2HEXD(*(s + 2));
  158.      s += 2;
  159.       }
  160.    }
  161.    *p = '\0';
  162. }
  163.  
  164. /* Encodes the unsafe characters (listed in URL_UNSAFE) in a given
  165.    string, returning a malloc-ed %XX encoded string. */
  166. char *
  167. encode_string(const char *s)
  168. {
  169.    const char *b;
  170.    char *p, *res;
  171.    int i;
  172.  
  173.    b = s;
  174.    for (i = 0; *s; s++, i++)
  175.       if (strchr(URL_UNSAFE, *s))
  176.      i += 2; /* Two more characters (hex digits) */
  177.    res = (char *)nmalloc(i + 1);
  178.    s = b;
  179.    for (p = res; *s; s++)
  180.       if (strchr(URL_UNSAFE, *s))
  181.       {
  182.      *p++ = '%';
  183.      *p++ = HEXD2ASC(*s >> 4);
  184.      *p++ = HEXD2ASC(*s & 0xf);
  185.       }
  186.       else
  187.      *p++ = *s;
  188.    *p = '\0';
  189.    return res;
  190. }
  191.  
  192. /* Returns the proto-type if it is a supported protocol, or URLUNKNOWN
  193.    if not. */
  194. uerr_t
  195. urlproto(const char *url)
  196. {
  197.    int i;
  198.  
  199.    url += skip_url(url);
  200.    for (i = 0; sup_protos[i].name; i++)
  201.       if (!strncasecmp(url, sup_protos[i].name, strlen(sup_protos[i].name)))
  202.      return sup_protos[i].ind;
  203.    for (i = 0; url[i] && url[i] != ':' && url[i] != '/'; i++);
  204.    if (url[i] == ':')
  205.    {
  206.       for (++i; url[i] && url[i] != '/'; i++)
  207.      if (!isdigit(url[i]))
  208.         return URLBADPORT;
  209.       if (url[i - 1] == ':')
  210.      return URLFTP;
  211.       else
  212.      return URLHTTP;
  213.    }
  214.    else
  215.       return URLHTTP;
  216. }
  217.  
  218. /* Skip the protocol part of the URL, e.g. `http://'.  If no protocol
  219.    part is found, returns 0. */
  220. int
  221. skip_proto(const char *url)
  222. {
  223.    int i, l;
  224.  
  225.    for (i = 0; protostrings[i]; i++)
  226.       if (!strncasecmp(protostrings[i], url, strlen(protostrings[i])))
  227.      break;
  228.    if (!protostrings[i])
  229.       return 0;
  230.    l = strlen(protostrings[i]);
  231.    /* HTTP and FTP protocols are expected to yield exact host names
  232.       (i.e. the `//' part must be skipped, too).  */
  233.    if (!strcmp(protostrings[i], "http:") || !strcmp(protostrings[i], "ftp:"))
  234.       l += 2;
  235.    return l;
  236. }
  237.  
  238. /* Returns 1 if the URL begins with a protocol (supported or
  239.    unsupported), 0 otherwise. */
  240. int
  241. has_proto(const char *url)
  242. {
  243.    char **s;
  244.  
  245.    url += skip_url(url);
  246.    for (s = protostrings; *s; s++)
  247.       if (strncasecmp(url, *s, strlen(*s)) == 0)
  248.      return 1;
  249.    return 0;
  250. }
  251.  
  252. /* Skip the username and password, if present here.  The function
  253.    should be called *not* with the complete URL, but with the part
  254.    right after the protocol.
  255.  
  256.    If no username and password are found, return 0. */
  257. int
  258. skip_uname(const char *url)
  259. {
  260.    const char *p;
  261.    for (p = url; *p && *p != '/'; p++)
  262.       if (*p == '@')
  263.      break;
  264.    /* If a '@' was found before the first occurrence of '/', skip
  265.       it. */
  266.    if (*p == '@')
  267.       return p - url + 1;
  268.    else
  269.       return 0;
  270. }
  271.  
  272. /* Allocate a new urlinfo structure, fill it with default values and
  273.    return a pointer to it. */
  274. urlinfo *
  275. newurl(void)
  276. {
  277.    urlinfo *u;
  278.  
  279.    u = (urlinfo *)nmalloc(sizeof(urlinfo));
  280.    memset(u, 0, sizeof(*u));
  281.    u->proto = URLUNKNOWN;
  282.    return u;
  283. }
  284.  
  285. /* Perform a "deep" free of the urlinfo structure. The structure
  286.    should have been created with newurl, but need not have been
  287.    used. If free_pointer is non-0, free the pointer itself. */
  288. void
  289. freeurl(urlinfo *u, int complete)
  290. {
  291.    assert(u != NULL);
  292.    if (u->url)
  293.       free(u->url);
  294.    if (u->host)
  295.       free(u->host);
  296.    if (u->path)
  297.       free(u->path);
  298.    if (u->file)
  299.       free(u->file);
  300.    if (u->dir)
  301.       free(u->dir);
  302.    if (u->user)
  303.       free(u->user);
  304.    if (u->passwd)
  305.       free(u->passwd);
  306.    if (u->local)
  307.       free(u->local);
  308.    if (u->referer)
  309.       free(u->referer);
  310.    if (u->proxy)
  311.       freeurl(u->proxy, 1);
  312.    if (complete)
  313.       free(u);
  314.    return;
  315. }
  316.  
  317. /* Extract the given URL of the form
  318.    (http:|ftp:)//(user(:password)?@)?hostname(:port)?(/path)?
  319.    1. hostname (terminated with '/' or ':')
  320.    2. port number (terminated with '/'), or chosen for the protocol
  321.    3. dirname (everything after hostname)
  322.    Most errors are handled. No allocation is done, you must supply
  323.    pointers to allocated memory.
  324.    ...and a host of other stuff :-)
  325.  
  326.    - Recognizes hostname:dir/file for FTP and
  327.      hostname(:portnum)?/dir/file for HTTP.
  328.    - Parses the path to yield directory and file
  329.    - Parses the URL to yield the username and passwd (if present)
  330.    - Decodes the strings, in case they contain "forbidden" characters
  331.    - Writes the result to struct urlinfo
  332.  
  333.    If the argument STRICT is set, it recognizes only the canonical
  334.    form.  */
  335. uerr_t
  336. parseurl(const char *url, urlinfo *u, int strict)
  337. {
  338.    int i, l, abs_ftp;
  339.    int recognizable;            /* Recognizable URL is the one where
  340.                    the protocol name was explicitly
  341.                    named, i.e. it wasn't deduced from
  342.                    the URL format. */
  343.    uerr_t type;
  344.  
  345. #ifdef DEBUG
  346.    if (opt.debug)
  347.       fprintf(opt.lfile, "parseurl(\"%s\") -> ", url);
  348. #endif
  349.    url += skip_url(url);
  350.    recognizable = has_proto(url);
  351.    if (strict && !recognizable)
  352.       return URLUNKNOWN;
  353.    for (i = 0, l = 0; sup_protos[i].name; i++)
  354.    {
  355.       l = strlen(sup_protos[i].name);
  356.       if (!strncasecmp(sup_protos[i].name, url, l))
  357.      break;
  358.    }
  359.    /* If protocol is recognizable, but unsupported, bail out, else
  360.       suppose unknown. */
  361.    if (recognizable && !sup_protos[i].name)
  362.       return URLUNKNOWN;
  363.    else if (!sup_protos[i].name)
  364.       type = URLUNKNOWN;
  365.    else
  366.       u->proto = type = sup_protos[i].ind;
  367.    
  368.    if (type == URLUNKNOWN)
  369.       l = 0;
  370.    /* Allow a username and password to be specified (i.e. just skip
  371.       them for now). */
  372.    if (recognizable)
  373.       l += skip_uname(url + l);
  374.    for (i = l; url[i] && url[i] != ':' && url[i] != '/'; i++);
  375.    if (i == l)
  376.       return URLBADHOST;
  377.    /* Get the hostname. */
  378.    u->host = strdupdelim(url + l, url + i);
  379. #ifdef DEBUG
  380.    if (opt.debug)
  381.       fprintf(opt.lfile, "host %s -> ", u->host);
  382. #endif
  383.    
  384.    /* Assume no port given. */
  385.    u->port = 0;
  386.    if (url[i] == ':')
  387.    {
  388.       /* We have a colon delimiting the hostname. It could mean that a
  389.      port number is following it, or a directory. */
  390.       if (isdigit(url[++i]))    /* A port number */
  391.       {
  392.      if (type == URLUNKNOWN)
  393.         u->proto = type = URLHTTP;
  394.      for (; url[i] && url[i] != '/'; i++)
  395.         if (isdigit(url[i]))
  396.            u->port = 10 * u->port + (url[i] - '0');
  397.         else
  398.            return URLBADPORT;
  399.      if (!u->port)
  400.         return URLBADPORT;
  401. #ifdef DEBUG
  402.      if (opt.debug)
  403.         fprintf(opt.lfile, "port %hu -> ", u->port);
  404. #endif
  405.       }
  406.       else if (type == URLUNKNOWN) /* Or a directory. */
  407.      u->proto = type = URLFTP;
  408.       else                      /* Or plain misformed port number */
  409.      return URLBADPORT;
  410.    }
  411.    else if (type == URLUNKNOWN)
  412.       u->proto = type = URLHTTP;
  413.    if (!u->port)
  414.    {
  415.       int i;
  416.       for (i = 0; sup_protos[i].name; i++)
  417.      if (sup_protos[i].ind == type)
  418.         break;
  419.       if (!sup_protos[i].name)
  420.      return URLUNKNOWN;
  421.       u->port = sup_protos[i].port;
  422.    }
  423.    /* Some delimiter troubles... */
  424.    if (url[i] == '/' && url[i - 1] != ':')
  425.       ++i;
  426.    if (type == URLHTTP)
  427.       while (url[i] && url[i] == '/')
  428.      ++i;
  429.    u->path = nmalloc(strlen(url + i) + 8);
  430.    strcpy(u->path, url + i);
  431. #ifdef DEBUG
  432.    if (opt.debug)
  433.       fprintf(opt.lfile, "opath %s -> ", u->path);
  434. #endif
  435.    /* Parse the username and password (if existing). */
  436.    parse_uname(url, &u->user, &u->passwd);
  437.    /* Decode the strings, as per RFC 1738. */
  438.    decode_string(u->host);
  439.    decode_string(u->path);
  440.    if (u->user)
  441.       decode_string(u->user);
  442.    if (u->passwd)
  443.       decode_string(u->passwd);
  444.    /* Parse the directory. */
  445.    parse_dir(u->path, &u->dir, &u->file);
  446. #ifdef DEBUG
  447.    if (opt.debug)
  448.       fprintf(opt.lfile, "dir %s -> file %s -> ", u->dir, u->file);
  449. #endif
  450.    /* Simplify the directory. */
  451.    path_simplify(u->dir);
  452.    /* Remove the leading `/' in HTTP. */
  453.    if (type == URLHTTP && *u->dir == '/')
  454.       strcpy(u->dir, u->dir + 1);
  455. #ifdef DEBUG
  456.    if (opt.debug)
  457.       fprintf(opt.lfile, "ndir %s\n", u->dir);
  458. #endif
  459.    /* Strip trailing '/'. */
  460.    l = strlen(u->dir);
  461.    if (l && u->dir[l - 1] == '/')
  462.       u->dir[l - 1] = '\0';
  463.    /* Re-create the path: */
  464.    abs_ftp = (u->proto == URLFTP && *u->dir == '/');
  465.    /*  sprintf(u->path, "%s%s%s%s", abs_ftp ? "%2F": "/",
  466.        abs_ftp ? (u->dir + 1) : u->dir, *u->dir ? "/" : "", u->file); */
  467.    strcpy(u->path, abs_ftp ? "%2F" : "/");
  468.    strcat(u->path, abs_ftp ? (u->dir + 1) : u->dir);
  469.    strcat(u->path, *u->dir ? "/" : "");
  470.    strcat(u->path, u->file);
  471.    URL_CLEANSE(u->path);
  472.    /* Create the clean URL. */
  473.    u->url = str_url(u, 0);
  474.    return URLOK;
  475. }
  476.  
  477. /* Build the directory and filename components of the path. Both
  478.    components are *separately* malloc-ed strings! It does not change
  479.    the contents of path.
  480.  
  481.    If the path ends with "." or "..", they are (correctly) counted as
  482.    directories. */
  483. void
  484. parse_dir(const char *path, char **dir, char **file)
  485. {
  486.    int i, l;
  487.  
  488.    for (i = l = strlen(path); i && path[i] != '/'; i--);
  489.    if (!i && *path != '/')   /* Just filename */
  490.    {
  491.       if (ISDOT(path) || ISDDOT(path))
  492.       {
  493.      *dir = nstrdup(path);
  494.      *file = nstrdup("");
  495.       }
  496.       else
  497.       {
  498.      *dir = nstrdup("");     /* This is required because of FTP */
  499.      *file = nstrdup(path);
  500.       }
  501.    }
  502.    else if (!i)                 /* /filename */
  503.    {
  504.       if (ISDOT(path + 1) || ISDDOT(path + 1))
  505.       {
  506.      *dir = nstrdup(path);
  507.      *file = nstrdup("");
  508.       }
  509.       else
  510.       {
  511.      *dir = nstrdup("/");
  512.      *file = nstrdup(path + 1);
  513.       }
  514.    }
  515.    else /* Nonempty directory with or without a filename */
  516.    {
  517.       if (ISDOT(path + i + 1) || ISDDOT(path + i + 1))
  518.       {
  519.      *dir = nstrdup(path);
  520.      *file = nstrdup("");
  521.       }
  522.       else
  523.       {
  524.      *dir = strdupdelim(path, path + i);
  525.      *file = strdupdelim(path + i + 1, path + l + 1);
  526.       }
  527.    }
  528. }
  529.  
  530. /* Find the optional username and password within the URL, as per
  531.    RFC1738. The returned user and passwd char pointers are
  532.    malloc-ed. */
  533. uerr_t
  534. parse_uname(const char *url, char **user, char **passwd)
  535. {
  536.    int l;
  537.    const char *p, *col;
  538.    char **where;
  539.  
  540.    *user = NULL;
  541.    *passwd = NULL;
  542.    url += skip_url(url);
  543.    /* Look for end of protocol string. */
  544.    l = skip_proto(url);
  545.    if (!l)
  546.       return URLUNKNOWN;
  547.    /* Add protocol offset. */
  548.    url += l;
  549.    /* Is there an '@' sign? */
  550.    for (p = url; *p && *p != '/'; p++)
  551.       if (*p == '@')
  552.      break;
  553.    /* If not, return. */
  554.    if (*p != '@')
  555.       return URLOK;
  556.    /* Else find the username and password. */
  557.    for (p = col = url; *p != '@'; p++)
  558.    {
  559.       if (*p == ':' && !*user)
  560.       {
  561.      *user = (char *)nmalloc(p - url + 1);
  562.      memcpy(*user, url, p - url);
  563.      (*user)[p - url] = '\0';
  564.      col = p + 1;
  565.       }
  566.    }
  567.    /* Decide whether you have only the username or both. */
  568.    where = *user ? passwd : user;
  569.    *where = (char *)nmalloc(p - col + 1);
  570.    memcpy(*where, col, p - col);
  571.    (*where)[p - col] = '\0';
  572.    return URLOK;
  573. }
  574.  
  575. /* Return the URL as fine-formed string, with a proper protocol, port
  576.    number, directory and optional user/password. If the hide is != 0,
  577.    the password will be hidden. The forbidden characters in the URL
  578.    will be cleansed. */
  579. char *
  580. str_url(const urlinfo *u, int hide)
  581. {
  582.    char *res, *host, *user, *passwd, *proto_name, *dir, *file;
  583.    int i, l, ln, lu, lh, lp, lf, ld;
  584.    
  585.    /* Look for the protocol name. */
  586.    for (i = 0; sup_protos[i].name; i++)
  587.       if (sup_protos[i].ind == u->proto)
  588.      break;
  589.    if (!sup_protos[i].name)
  590.       return NULL;
  591.    proto_name = sup_protos[i].name;
  592.    host = CLEANDUP(u->host);
  593.    dir = CLEANDUP(u->dir);
  594.    file = CLEANDUP(u->file);
  595.    user = passwd = NULL;
  596.    if (u->user)
  597.       user = CLEANDUP(u->user);
  598.    if (u->passwd)
  599.    {
  600.       int i;
  601.       passwd = CLEANDUP(u->passwd);
  602.       if (hide)
  603.      for (i = 0; passwd[i]; i++)
  604.         passwd[i] = 'x';
  605.    }
  606.    if (u->proto == URLFTP && *dir == '/')
  607.    {
  608.       char *tmp = nmalloc(strlen(dir) + 3);
  609.       /*sprintf(tmp, "%%2F%s", dir + 1);*/
  610.       *tmp = '%';
  611.       tmp[1] = '2';
  612.       tmp[2] = 'F';
  613.       strcpy(tmp + 3, dir + 1);
  614.       free(dir);
  615.       dir = tmp;
  616.    }
  617.  
  618.    ln = strlen(proto_name);
  619.    lu = user ? strlen(user) : 0;
  620.    lp = passwd ? strlen(passwd) : 0;
  621.    lh = strlen(host);
  622.    ld = strlen(dir);
  623.    lf = strlen(file);
  624.    res = (char *)nmalloc(ln + lu + lp + lh + ld + lf + 20); /* Safe sex. */
  625.    /* sprintf(res, "%s%s%s%s%s%s:%d/%s%s%s", proto_name,
  626.       (user ? user : ""), (passwd ? ":" : ""),
  627.       (passwd ? passwd : ""), (user ? "@" : ""),
  628.       host, u->port, dir, *dir ? "/" : "", file); */
  629.    l = 0;
  630.    memcpy(res, proto_name, ln);
  631.    l += ln;
  632.    if (user)
  633.    {
  634.       memcpy(res + l, user, lu);
  635.       l += lu;
  636.       if (passwd)
  637.       {
  638.      res[l++] = ':';
  639.      memcpy(res + l, passwd, lp);
  640.      l += lp;
  641.       }
  642.       res[l++] = '@';
  643.    }
  644.    memcpy(res + l, host, lh);
  645.    l += lh;
  646.    res[l++] = ':';
  647.    prnum(res + l, (long)u->port);
  648.    l += numdigit(u->port);
  649.    res[l++] = '/';
  650.    memcpy(res + l, dir, ld);
  651.    l += ld;
  652.    if (*dir)
  653.       res[l++] = '/';
  654.    strcpy(res + l, file);
  655.    free(host);
  656.    free(dir);
  657.    free(file);
  658.    if (user)
  659.       free(user);
  660.    if (passwd)
  661.       free(passwd);
  662.    return res;
  663. }
  664.  
  665. /* Check whether two URL-s are equivalent, i.e. pointing to the same
  666.    location.  Uses parseurl to parse them, and compares the canonical
  667.    forms.
  668.  
  669.    Returns 1 if the URL1 is equivalent to URL2, 0 otherwise.  Also
  670.    return 0 on error.  */
  671. int
  672. url_equal(const char *url1, const char *url2)
  673. {
  674.    urlinfo *u1, *u2;
  675.    uerr_t err;
  676.    int res;
  677.  
  678.    u1 = newurl();
  679.    err = parseurl(url1, u1, 0);
  680.    if (err != URLOK)
  681.    {
  682.       freeurl(u1, 1);
  683.       return 0;
  684.    }
  685.    u2 = newurl();
  686.    err = parseurl(url2, u2, 0);
  687.    if (err != URLOK)
  688.    {
  689.       freeurl(u2, 1);
  690.       return 0;
  691.    }
  692.    res = !strcmp(u1->url, u2->url);
  693.    freeurl(u1, 1);
  694.    freeurl(u2, 1);
  695.    return res;
  696. }
  697.  
  698. /* Find URL of format scheme:hostname[:port]/dir in a buffer. The
  699.    buffer may contain anything, the routine should not bug out. */
  700. const char *
  701. findurl(const char *buf, int howmuch, int *count)
  702. {
  703.    char **prot;
  704.    const char *s1, *s2;
  705.  
  706.    for (s1 = buf; howmuch; s1++, howmuch--)
  707.       for (prot = protostrings; *prot; prot++)
  708.      if (howmuch <= strlen(*prot))
  709.         continue;
  710.      else if (!strncasecmp(*prot, s1, strlen(*prot)))
  711.      {
  712.         for (s2 = s1, *count = 0;
  713.          howmuch && *s2 && *s2 >= 32 && *s2 < 127 && !isspace(*s2) &&
  714.             !strchr(URL_SEPARATOR, *s2);
  715.          s2++, (*count)++, howmuch--);
  716.         return s1;
  717.      }
  718.    return NULL;
  719. }
  720.  
  721. /* Scans the file for signs of URL-s. Returns a vector of pointers,
  722.    each pointer representing a URL string. The file is *not* HTML. */
  723. urlpos *
  724. get_urls_file(const char *file)
  725. {
  726.    long nread;
  727.    FILE *fp;
  728.    char *buf;
  729.    const char *pbuf;
  730.    int size;
  731.    urlpos *first, *current, *old;
  732.  
  733.    if (!file || strcmp(file, "-"))
  734.    {
  735.       fp = fopen(file, "r");
  736.       if (!fp)
  737.       {
  738.      if (!opt.quiet)
  739.         fprintf(opt.lfile, "%s: %s\n", file, mystrerror(errno));
  740.      return NULL;
  741.       }
  742.    }
  743.    else
  744.       fp = stdin;
  745.    /* Load the file. */
  746.    load_file(fp, &buf, &nread);
  747.    if (file || (*file == '-' && !*(file + 1)))
  748.       fclose(fp);
  749. #ifdef DEBUG
  750.    if (opt.debug)
  751.       fprintf(opt.lfile, "Loaded %s (size %ld).\n", file, nread);
  752. #endif
  753.    first = current = NULL;
  754.    /* Fill the linked list with URLs. */
  755.    for (pbuf = buf; (pbuf = findurl(pbuf, nread - (pbuf - buf), &size));
  756.     pbuf += size)
  757.    {
  758.       /* Allocate the space. */
  759.       old = current;
  760.       current = (urlpos *)nmalloc(sizeof(urlpos));
  761.       if (old)
  762.      old->next = current;
  763.       memset(current, 0, sizeof(*current));
  764.       current->next = NULL;
  765.       current->url = (char *)nmalloc(size + 1);
  766.       memcpy(current->url, pbuf, size);
  767.       current->url[size] = '\0';
  768.       if (!first)
  769.      first = current;
  770.    }
  771.    /* Free the buffer. */
  772.    free(buf);
  773.  
  774.    return first;
  775. }
  776.  
  777. /* Similar to get_urls_file, but for HTML files. The files are scanned
  778.    as valid HTML documents -- see htmlfindurl for details on what gets
  779.    picked up. get_urls_html constructs the HTML-s from the relative
  780.    href-s.
  781.  
  782.    If flag is set, it will not barf on baseless relative links.  */
  783. urlpos *
  784. get_urls_html(const char *file, const char *this_url, int silent)
  785. {
  786.    long nread;
  787.    FILE *fp;
  788.    char *buf, *constr, *base;
  789.    const char *pbuf, *cbase;
  790.    int i, size, no_proto, skip_blanks, first_time;
  791.    urlpos *first, *current, *old;
  792.  
  793.    if (!file || strcmp(file, "-"))
  794.    {
  795.       fp = fopen(file, "r");
  796.       if (!fp)
  797.       {
  798.      if (!opt.quiet)
  799.         fprintf(opt.lfile, "%s: %s\n", file, mystrerror(errno));
  800.      return NULL;
  801.       }
  802.    }
  803.    else
  804.       fp = stdin;
  805.    /* Load the file. */
  806.    load_file(fp, &buf, &nread);
  807.    fclose(fp);
  808. #ifdef DEBUG
  809.    if (opt.debug)
  810.       fprintf(opt.lfile, "Loaded HTML file %s (size %ld).\n", file, nread);
  811. #endif
  812.    first = current = NULL;
  813.    first_time = 1;
  814.    /* htmlfindurl is the HTML parser that returns the next URL. */
  815.    for (pbuf = buf; (pbuf = htmlfindurl((unsigned char *)pbuf,
  816.                     nread - (pbuf - buf),
  817.                     &size, first_time));
  818.     pbuf += size)
  819.    {
  820.       if (first_time)
  821.      first_time = 0;
  822.       /* This is a simple mechanism for brain-damaged pages that refer
  823.      to URI-s as <a href="<spaces>URI">. If the URI is absolute,
  824.      the spaces will be silently skipped. Otherwise, the spaces
  825.      will still be taken for a legal part of a relative URI. Note
  826.      that you can still write <a href = any_URI> without spaces
  827.      having any special meaning. Thanks to Hrvoje Lacko
  828.      <hlacko@fly.cc.fer.hr>. */
  829.       for (skip_blanks = 0; isspace(pbuf[skip_blanks]) && skip_blanks < size;
  830.        skip_blanks++);
  831.       for (i = 0; protostrings[i]; i++)
  832.       {
  833.      if (!strncasecmp(protostrings[i], pbuf + skip_blanks,
  834.                MINVAL(strlen(protostrings[i]),
  835.                   size - skip_blanks)))
  836.         break;
  837.       }
  838.       /* The second part of the check is provided for bd pages
  839.          refering to http:URL.  See below for details.  */
  840.       if (protostrings[i]
  841.       && !(strncasecmp(pbuf + skip_blanks, "http:", 5) == 0
  842.            && strncasecmp(pbuf + skip_blanks, "http://", 7) != 0))
  843.       {
  844.      no_proto = 0;
  845.       }
  846.       else
  847.       {
  848.      no_proto = 1;
  849.      /* This is for extremely brain-damaged pages that refer to
  850.         relative URI-s as <a href="http:URL">.  Just strip off the
  851.         silly leading "http:" (as well as any leading blanks
  852.         before it).  */
  853.      if ((size > skip_blanks + 5) &&
  854.          !strncasecmp("http:", pbuf + skip_blanks, 5))
  855.      {
  856.         pbuf += skip_blanks + 5;
  857.         size -= skip_blanks + 5;
  858.      }
  859.       }
  860.       if (!no_proto && skip_blanks)
  861.       {
  862.      pbuf += skip_blanks;
  863.      size -= skip_blanks;
  864.       }
  865.       if (!no_proto)
  866.       {
  867.      for (i = 0; sup_protos[i].name; i++)
  868.      {
  869.         if (!strncasecmp(sup_protos[i].name, pbuf,
  870.                  MINVAL(strlen(sup_protos[i].name), size)))
  871.            break;
  872.      }
  873.      /* Do *not* accept a non-supported protocol. */
  874.      if (!sup_protos[i].name)
  875.         continue;
  876.       }
  877.       if (no_proto)
  878.       {
  879.      /* First, construct the base, which can be relative itself.
  880.  
  881.         Criteria for creating the base are:
  882.         1) html_base created by <base href="...">
  883.         2) current URL
  884.         3) base provided from the command line */
  885.      base = NULL;
  886.      cbase = html_base();
  887.      if (!cbase)
  888.         cbase = this_url;
  889.      if (!cbase)
  890.         cbase = opt.base_href;
  891.      if (!cbase)             /* Error condition -- a baseless
  892.                     relative link. */
  893.      {
  894.         if (!opt.quiet && !silent)
  895.         {
  896.            char *temp = (char *)nmalloc(size + 1);
  897.            strncpy(temp, pbuf, size);
  898.            temp[size] = '\0';
  899.            fprintf(opt.lfile,
  900.                "Error (%s): Link %s without a base provided.\n",
  901.                file, temp);
  902.            free(temp);
  903.         }
  904.         continue;
  905.      }
  906.      if (this_url)
  907.         base = construct(this_url, cbase, strlen(cbase),
  908.                  !has_proto(cbase));
  909.      else
  910.      {
  911.         /* Base must now be absolute, with host name and
  912.            protocol. */
  913.         if (!has_proto(cbase))
  914.         {
  915.            if (!opt.quiet)
  916.            {
  917.           fprintf(opt.lfile,
  918.               "Error (%s): Base %s relative, without referer URL.\n",
  919.               file, cbase);
  920.            }
  921.            continue;
  922.         }
  923.         base = nstrdup(cbase);
  924.      }
  925.      constr = construct(base, pbuf, size, no_proto);
  926.      free(base);
  927.       }
  928.       else /* has proto */
  929.       {
  930.      constr = (char *)nmalloc(size + 1);
  931.      strncpy(constr, pbuf, size);
  932.      constr[size] = '\0';
  933.       }
  934. #ifdef DEBUG
  935.       if (opt.debug)
  936.       {
  937.      char *tmp;
  938.      const char *tmp2;
  939.      
  940.      tmp2 = html_base();
  941.      tmp = (char *)nmalloc(size + 1);
  942.      strncpy(tmp, pbuf, size);
  943.      tmp[size] = '\0';
  944.      fprintf(opt.lfile,
  945.          "file %s; this_url %s; base %s\nlink: %s; constr: %s\n",
  946.          file, this_url ? this_url : "(null)",
  947.          tmp2 ? tmp2 : "(null)", tmp, constr);
  948.      free(tmp);
  949.       }
  950. #endif
  951.  
  952.       /* Allocate the space. */
  953.       old = current;
  954.       current = (urlpos *)nmalloc(sizeof(urlpos));
  955.       if (old)
  956.      old->next = current;
  957.       if (!first)
  958.      first = current;
  959.       /* Fill the values. */
  960.       memset(current, 0, sizeof(*current));
  961.       current->next = NULL;
  962.       current->url = constr;
  963.       current->size = size;
  964.       current->pos = pbuf - buf;
  965.       /* A URL is relative if the host and protocol are not named,
  966.      and the name does not start with '/'. */
  967.       if (no_proto && *pbuf != '/')
  968.      current->flags |= (URELATIVE | UNOPROTO);
  969.       else if (no_proto)
  970.      current->flags |= UNOPROTO;
  971.    }
  972.    /* Free the buffer. */
  973.    free(buf);
  974.  
  975.    return first;
  976. }
  977.  
  978. /* Free the linked list of urlpos. */
  979. void
  980. free_urlpos(urlpos *l)
  981. {
  982.    urlpos *next;
  983.    
  984.    while (l)
  985.    {
  986.       next = l->next;
  987.       free(l->url);
  988.       if (l->local_name)
  989.      free(l->local_name);
  990.       free(l);
  991.       l = next;
  992.    }
  993. }
  994.  
  995. /* Create all the necessary directories for PATH (a file).  Calls
  996.    mymkdir internally.  */
  997. int
  998. mkalldirs(const char *path)
  999. {
  1000.    const char *p;
  1001.    char *t;
  1002.    struct stat st;
  1003.    int res;
  1004.  
  1005.    p = path + strlen(path);
  1006.    for (; *p != '/' && p != path; p--);
  1007.    /* Don't create if it's just a file.  */
  1008.    if ((p == path) && (*p != '/'))
  1009.       return 0;
  1010.    t = strdupdelim(path, p);
  1011.    /* Check whether the directory exists. */
  1012.    if ((stat(t, &st) == 0))
  1013.    {
  1014.       if (S_ISDIR(st.st_mode))
  1015.       {
  1016.      free(t);
  1017.      return 0;
  1018.       }
  1019.       else
  1020.       {
  1021.      /* If the dir exists as a file name, remove it first.  This
  1022.       is *only* for Wget to work with buggy buggy buggy http
  1023.       servers. This situation will *not* occur when contacting a
  1024.       normal server.  */
  1025.      DEBUGP("Removing because of directory danger!\n");
  1026.      unlink(t);
  1027.       }
  1028.    }
  1029.    res = mymkdir(t);
  1030.    if (res != 0)
  1031.    {
  1032.       if (!opt.quiet)
  1033.      fprintf(opt.lfile, "%s: %s", t, mystrerror(errno));
  1034.    }
  1035.    free(t);
  1036.    return res;
  1037. }
  1038.  
  1039. /* Return the path name of the URL-equivalent file name, with a
  1040.    remote-like structure of directories.  */
  1041. char *
  1042. mkstruct(const urlinfo *u)
  1043. {
  1044.    char *host, *nhost, *dir, *file, *res, *dirpref;
  1045.    int l;
  1046.  
  1047.    assert(u->dir != NULL);
  1048.    assert(u->host != NULL);
  1049.  
  1050.    host = nstrdup(u->host);
  1051.    /* Let's check for a host's true name (or at least a consistent
  1052.       name for saving to directory), reusing the hlist if possible. */
  1053.    if (opt.add_hostdir && !opt.simple_check)
  1054.    {
  1055.       nhost = realhost(host);
  1056.       free(host);
  1057.       host = nhost;
  1058.    }
  1059.    /* Add dir_prefix and hostname (if required) to the beginning of
  1060.       dir. */
  1061.    if (opt.add_hostdir)
  1062.    {
  1063.       if (!ISDOT(opt.dir_prefix))
  1064.       {
  1065.      dirpref = nmalloc(strlen(opt.dir_prefix) + 1 + strlen(host) + 1);
  1066.      sprintf(dirpref, "%s/%s", opt.dir_prefix, host);
  1067.       }
  1068.       else
  1069.      dirpref = nstrdup(host);
  1070.    }
  1071.    else                         /* not add_hostdir */
  1072.    {
  1073.       if (!ISDOT(opt.dir_prefix))
  1074.      dirpref = nstrdup(opt.dir_prefix);
  1075.       else
  1076.      dirpref = nstrdup("");
  1077.    }
  1078.    free(host);
  1079.  
  1080.    /* If there is a prefix, prepend it. */
  1081.    if (*dirpref)
  1082.    {
  1083.       dir = (char *)nmalloc(strlen(dirpref) + 1 + strlen(u->dir) + 2);
  1084.       sprintf(dir, "%s%s%s", dirpref, *u->dir == '/' ? "" : "/", u->dir);
  1085.    }
  1086.    else  /* Just make it the directory without the leading '/'. */
  1087.       dir = nstrdup(u->dir + (*u->dir == '/' ? 1 : 0));
  1088.    free(dirpref);
  1089.    URL_CLEANSE(dir);
  1090.    l = strlen(dir);
  1091.    if (l && dir[l - 1] == '/')
  1092.       dir[l - 1] = '\0';
  1093.  
  1094.    if (!*u->file)
  1095.       file = "index.html";
  1096.    else
  1097.       file = u->file;
  1098.    /* Finally, construct the full name. */
  1099.    res = (char *)nmalloc(strlen(dir) + 1 + strlen(file) + 1);
  1100.    sprintf(res, "%s%s%s", dir, *dir ? "/" : "", file);
  1101.    free(dir);
  1102.    return res;
  1103. }
  1104.  
  1105. /* Create a unique filename, corresponding to a given URL.  Calls
  1106.    mkstruct if necessary.  Does *not* actually create any directories.  */
  1107. char *
  1108. url_filename(const urlinfo *u)
  1109. {
  1110.    char *file, *name;
  1111.    int count, have_prefix;
  1112.  
  1113.    have_prefix = 0;             /* Must we append the dir_prefix? */
  1114.    if (opt.dirstruct)
  1115.    {
  1116.       file = mkstruct(u);
  1117.       have_prefix = 1;
  1118.    }
  1119.    else
  1120.    {
  1121.       if (!*u->file)
  1122.      file = nstrdup("index.html");
  1123.       else
  1124.      file = nstrdup(u->file);
  1125.    }
  1126.  
  1127.    if (!have_prefix)
  1128.    {
  1129.       /* Check whether the prefix directory is something other than "."
  1130.      before prepending it. */
  1131.       if (!ISDOT(opt.dir_prefix))
  1132.       {
  1133.      char *nfile = (char *)nmalloc(strlen(opt.dir_prefix)
  1134.                        + 1 + strlen(file) + 1);
  1135.      sprintf(nfile, "%s/%s", opt.dir_prefix, file);
  1136.      free(file);
  1137.      file = nfile;
  1138.       }
  1139.    }
  1140.    /* DOS-ish file systems don't like `%' signs in them; we change it
  1141.       to `@'.  */
  1142. #ifdef WINDOWS
  1143.    do {
  1144.       char *p = file;
  1145.       for (p = file; *p; p++)
  1146.      if (*p == '%')
  1147.         *p = '@';
  1148.    }
  1149. #endif /* WINDOWS */
  1150.    
  1151.    /* Check the cases in which the extensions are not used:
  1152.       1) Clobbering is turned off (-nc).
  1153.       2) Retrieval with regetting.
  1154.       3) Timestamping is used.
  1155.       4) Hierarchy is built.
  1156.  
  1157.       The exception is the case when file does exist and is a
  1158.       directory (actually support for bad httpd-s). */
  1159.    if ((opt.noclobber || opt.always_rest || opt.timestamping || opt.dirstruct)
  1160.        && !(exists(file) && !isfile(file)))
  1161.       return file;
  1162.  
  1163.    /* Find a unique name.  */
  1164.    for (count = 0; !(name = unique_name(file, count)); count++)
  1165.       ;
  1166.    free(file);
  1167.    return name;
  1168. }
  1169.  
  1170. /* Return a unique filename, given a prefix and count */
  1171. char *
  1172. unique_name(const char *fileprefix, int count)
  1173. {
  1174.    char *filename;
  1175.  
  1176.    if (count)
  1177.    {
  1178.       filename = (char *)nmalloc(strlen(fileprefix) + numdigit(count) + 2);
  1179.       sprintf(filename, "%s.%d", fileprefix, count);
  1180.    }
  1181.    else
  1182.       filename = nstrdup(fileprefix);
  1183.    if (!exists(filename))
  1184.       return filename;
  1185.    else
  1186.    {
  1187.       free(filename);
  1188.       return NULL;
  1189.    }
  1190. }
  1191.  
  1192. /* Construct an absolute URL, given a (possibly) relative one.  This
  1193.    is more tricky than it might seem, but it works. */
  1194. char *
  1195. construct(const char *url, const char *sub, int subsize, int no_proto)
  1196. {
  1197.    int i, fl;
  1198.    char *constr, *t;
  1199.  
  1200.    t = NULL;
  1201.    if (no_proto)
  1202.    {
  1203.       if (*sub != '/')
  1204.       {
  1205.      for (i = strlen(url); i && url[i] != '/'; i--);
  1206.      if (!i || (url[i] == url[i - 1]))
  1207.      {
  1208.         int l;
  1209.         t = (char *)nmalloc((l = strlen(url)) + 2);
  1210.         strcpy(t, url);
  1211.         t[l] = '/';
  1212.         t[l + 1] = '\0';
  1213.         url = t;
  1214.         i = l;
  1215.      }
  1216.      constr = (char *)nmalloc(i + 1 + subsize + 1);
  1217.      strncpy(constr, url, i + 1);
  1218.      constr[i + 1] = '\0';
  1219.      strncat(constr, sub, subsize);
  1220.       }
  1221.       else
  1222.       {
  1223.      i = 0;
  1224.      do
  1225.      {
  1226.         for (; url[i] && url[i] != '/'; i++);
  1227.         if (!url[i])
  1228.            break;
  1229.         if ((fl = (url[i] == url[i + 1] && url[i + 1] == '/')))
  1230.            i += 2;
  1231.      } while (fl);
  1232.      if (!url[i])
  1233.      {
  1234.         int l;
  1235.         t = (char *)nmalloc((l = strlen(url)) + 2);
  1236.         strcpy(t, url);
  1237.         t[l] = '/';
  1238.         t[l + 1] = '\0';
  1239.         url = t;
  1240.      }
  1241.      constr = (char *)nmalloc(i + 1 + subsize + 1);
  1242.      strncpy(constr, url, i);
  1243.      constr[i] = '\0';
  1244.      strncat(constr + i, sub, subsize);
  1245.      constr[i + subsize] = '\0';
  1246.       }
  1247.    }
  1248.    else
  1249.    {
  1250.       constr = (char *)nmalloc(subsize + 1);
  1251.       strncpy(constr, sub, subsize);
  1252.       constr[subsize] = '\0';
  1253.    }
  1254.    if (t)
  1255.       free(t);
  1256.    return constr;
  1257. }
  1258.  
  1259.  
  1260. /* URL is optimized by host. The data in urlinfo* IS changed! */
  1261. void
  1262. opt_url(urlinfo *u)
  1263. {
  1264.    char *host;
  1265.    
  1266.    assert(u->dir != NULL);      /* The URL must be parsed */
  1267.  
  1268.    /* Find the "true" host. */
  1269.    host = realhost(u->host);
  1270.    free(u->host);
  1271.    u->host = host;
  1272.    /* Refresh the struct. */
  1273.    free(u->url);
  1274.    u->url = str_url(u, 0);
  1275. }
  1276.  
  1277. /* Returns proxy host address, according to protocol. */
  1278. char *
  1279. getproxy(uerr_t proto)
  1280. {
  1281.    if (proto == URLHTTP)
  1282.       return opt.http_proxy ? opt.http_proxy : getenv("http_proxy");
  1283.    else if (proto == URLFTP)
  1284.       return opt.ftp_proxy ? opt.ftp_proxy : getenv("ftp_proxy");
  1285.    else
  1286.       return NULL;
  1287. }
  1288.  
  1289. /* Should a host be accessed through proxy, concerning no_proxy? */
  1290. int
  1291. no_proxy_match(const char *host, const char **no_proxy)
  1292. {
  1293.    if (!no_proxy)
  1294.       return 1;
  1295.    return !sufmatch(no_proxy, host);
  1296. }
  1297.  
  1298. /* Change the links in an HTML document.  Accepts a structure that
  1299.    defines the positions of all the links. */
  1300. void
  1301. convert_links(const char *file, urlpos *l)
  1302. {
  1303.    FILE *fp;
  1304.    char *buf, *p, *p2;
  1305.    char *newname;
  1306.    long size;
  1307.  
  1308.    if (opt.verbose)
  1309.       fprintf(opt.lfile, "Converting %s... ", file);
  1310.    /* Read from the file.... */
  1311.    fp = fopen(file, "r");
  1312.    if (!fp)
  1313.    {
  1314.       if (!opt.quiet)
  1315.      fprintf(opt.lfile, "Cannot convert links in %s: %s\n", file,
  1316.          mystrerror(errno));
  1317.       return;
  1318.    }
  1319.    /* ...to a buffer. */
  1320.    load_file(fp, &buf, &size);
  1321.    fclose(fp);
  1322.    /* Now open the file for writing. */
  1323.    fp = fopen(file, "w");
  1324.    if (!fp)
  1325.    {
  1326.       if (!opt.quiet)
  1327.      fprintf(opt.lfile, "Cannot convert links in %s: %s\n", file,
  1328.          mystrerror(errno));
  1329.       free(buf);
  1330.       return;
  1331.    }
  1332.    for (p = buf; l; l = l->next)
  1333.    {
  1334.       if (l->pos >= size)
  1335.       {
  1336.      DEBUGP("Something strange is going on. Please investigate.");
  1337.      break;
  1338.       }
  1339.       /* If the URL already is relative or it is not to be converted
  1340.      for some other reason (e.g. because of not having been
  1341.      downloaded in the first place), skip it. */
  1342.       if ((l->flags & URELATIVE) || !(l->flags & UABS2REL))
  1343.       {
  1344. #ifdef DEBUG
  1345.      if (opt.debug)
  1346.         fprintf(opt.lfile,
  1347.             "Skipping %s at position %d (flags %d).\n", l->url,
  1348.             l->pos, l->flags);
  1349. #endif
  1350.      continue;
  1351.       }
  1352.       /* Else, reach the position of the offending URL, echoing
  1353.      everything up to it to the outfile. */
  1354.       for (p2 = buf + l->pos; p < p2; p++)
  1355.      putc(*p, fp);
  1356.       if (l->flags & UABS2REL)
  1357.       {
  1358.      newname = construct_relative(file, l->local_name);
  1359.      fprintf(fp, "%s", newname);
  1360. #ifdef DEBUG
  1361.      if (opt.debug)
  1362.         fprintf(opt.lfile, "ABS2REL: %s to %s at position %d in %s.\n",
  1363.             l->url, newname, l->pos, file);
  1364. #endif
  1365.      free(newname);
  1366.       }
  1367.       p += l->size;
  1368.    }
  1369.    if (p - buf < size)
  1370.    {
  1371.       for (p2 = buf + size; p < p2; p++)
  1372.      putc(*p, fp);
  1373.    }
  1374.    fclose(fp);
  1375.    free(buf);
  1376.    if (opt.verbose)
  1377.       fprintf(opt.lfile, "done.\n");
  1378. }
  1379.  
  1380. /* This function constructs and returns a malloced copy of the
  1381.    relative link from two pieces of information: local name of the
  1382.    referring file (s1) and local name of the referred file (s2).
  1383.  
  1384.    So, if s1 is "jagor.srce.hr/index.html" and s2 is
  1385.    "jagor.srce.hr/images/news.gif", new name should be
  1386.    "images/news.gif".
  1387.  
  1388.    Alternately, if the s1 is "fly.cc.fer.hr/ioccc/index.html", and s2
  1389.    is "fly.cc.fer.hr/images/fly.gif", new name should be
  1390.    "../images/fly.gif".
  1391.  
  1392.    Caveats: s1 should not begin with '/', unless s2 begins with '/'
  1393.    too.  s1 should not contain things like ".." and such --
  1394.    construct_relative("fly/ioccc/../index.html", "fly/images/fly.gif")
  1395.    will fail.  (workaround is to call path_simplify on s1).  */
  1396. char *
  1397. construct_relative(const char *s1, const char *s2)
  1398. {
  1399.    int i, cnt, sepdirs1;
  1400.    char *res;
  1401.  
  1402.    if (*s2 == '/')
  1403.       return nstrdup(s2);
  1404.    /* s1 should *not* be absolute, if s2 wasn't. */
  1405.    assert (*s1 != '/');
  1406.    i = cnt = 0;
  1407.    /* Skip the directories common to both strings. */
  1408.    while (1)
  1409.    {
  1410.       for (; s1[i] && s2[i] && s1[i] == s2[i] && s1[i] != '/' && s2[i] != '/';
  1411.        i++);
  1412.       if (s1[i] == '/' && s2[i] == '/')
  1413.      cnt = ++i;
  1414.       else
  1415.      break;
  1416.    }
  1417.    for (sepdirs1 = 0; s1[i]; i++)
  1418.       if (s1[i] == '/')
  1419.      ++sepdirs1;
  1420.    /* Now, construct the file as of:
  1421.       - ../ repeated sepdirs1 time
  1422.       - all the non-mutual directories of s2. */
  1423.    res = (char *)nmalloc(3 * sepdirs1 + strlen(s2 + cnt) + 1);
  1424.    for (i = 0; i < sepdirs1; i++)
  1425.       memcpy(res + 3 * i, "../", 3);
  1426.    strcpy(res + 3 * i, s2 + cnt);
  1427.    return res;
  1428. }
  1429.  
  1430. /* Add a URL to the list.  */
  1431. urlpos *
  1432. add_url(urlpos *l, const char *url, const char *file)
  1433. {
  1434.    urlpos *t, *b;
  1435.    
  1436.    t = (urlpos *)nmalloc(sizeof(urlpos));
  1437.    memset(t, 0, sizeof(*t));
  1438.    t->url = nstrdup(url);
  1439.    t->local_name = nstrdup(file);
  1440.    if (!l)
  1441.       return t;
  1442.    b = l;
  1443.    while (l->next)
  1444.       l = l->next;
  1445.    l->next = t;
  1446.    return b;
  1447. }
  1448.